﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Telnet UI Application
 * Version 2.10
 * 
 * Connection Definition File Class
 *      Represents and manages the content of the connection definition file.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;
using System.Net;
using Adaptive.Foundation;
using Adaptive.Network;
using Adaptive.Network.Sockets;
using Adaptive.Telnet.UI.Properties;

namespace Adaptive.Telnet.UI
{
    /// <summary>
    /// Represents and manages the content of the connection definition file.
    /// </summary>
    public sealed class ConnectionDefinitionFile : DisposableObjectBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations

        private ConnectionDefinitionCollection _definitions;
        private string _pathAndFileName = string.Empty;

        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ConnectionDefinitionFile()
            : base()
        {
            _pathAndFileName = Path.Combine(Directory.GetCurrentDirectory(), Resources.DefaultFileName);
            _definitions = new ConnectionDefinitionCollection();
        }
        /// <summary>
        /// Initializes a new instance of the class for the specified file.
        /// </summary>
        /// <param name="pathAndFileName">
        /// The fully-qualified path and file name of the connection definitions file.
        /// </param>
        public ConnectionDefinitionFile(string pathAndFileName)
            : base()
        {
            _pathAndFileName = pathAndFileName;
            _definitions = new ConnectionDefinitionCollection();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_definitions != null)
                {
                    _definitions.ClearAndDispose();
                    _definitions.Dispose();
                }
            }

            _definitions = null;
            _pathAndFileName = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the path and name of the file to save to or read from.
        /// </summary>
        /// <value>
        /// A fully-qualified path and file name value.
        /// </value>
        public string PathAndFileName
        {
            get { return _pathAndFileName; }
            set { _pathAndFileName = value; }
        }
        /// <summary>
        /// Gets the reference to the list of connection definitions.
        /// </summary>
        /// <value>
        /// A <see cref="ConnectionDefinitionCollection"/> instance containing the list.
        /// </value>
        public ConnectionDefinitionCollection Definitions
        {
            get { return _definitions; }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public void Clear()
        {
            _definitions.Clear();
            _pathAndFileName = string.Empty;
        }
        /// <summary>
        /// Loads the content from disk.
        /// </summary>
        public void Load()
        {
            FileStream file = null;             //File stream.

            if (File.Exists(_pathAndFileName))
            {
                try
                {
                    file = new FileStream(_pathAndFileName, FileMode.Open, FileAccess.Read);
                }
                catch
                {
                    file = null;
                }
                if (file != null)
                {
                    _definitions.Load(file);
                    file.Close();
                    file.Dispose();
                }
            }
        }
        /// <summary>
        /// Saves the current content to disk.
        /// </summary>
        public void Save()
        {
            FileStream file = null;             //File stream.

            //Delete if present.
            if (File.Exists(_pathAndFileName))
            {
                try
                {
                    File.Delete(_pathAndFileName);
                }
                catch
                {
                }
            }

            //Create the file.
            try
            {
                file = new FileStream(_pathAndFileName, FileMode.CreateNew, FileAccess.Write);
            }
            catch
            {
                file = null;
            }
            if (file != null)
            {
                _definitions.Save(file);
                file.Close();
                file.Dispose();
            }

        }
        #endregion
    }
}